<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, viewport-fit=cover" name="viewport" />
    <title>发表回复</title>
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.4.1/weui.min.css">
    <style>
        h4{
            width: 95vw;
            margin: 10px auto;
        }
        .topTips-hidden{
            opacity: 0; display: none;
        }
        .topTips-show{
            display: block; opacity: 1;
        }

        #ft_button{
            border: 0px solid red;
            position: fixed;
            bottom: 0;
            width: 100vw;
            background-color: gray;
            color: #fff;
        }

    </style>
</head>
<body>

<div class="weui-toptips weui-toptips_warn topTips-hidden" id="topTips">错误提示</div>

<div>

    <h4></h4>

    <div id="wangEditor"></div>

</div>

<a href="javascript:" class="weui-btn_cell weui-btn_cell-primary" id="ft_button" onclick="fnPostReply();">
    发布
</a>

</body>

<script type="text/javascript" src="../js/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="../js/doT.min.js"></script>
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="https://unpkg.com/wangeditor/dist/wangEditor.min.js"></script>

<script type="text/javascript">

    var editor;

    $(document).ready(function(){

        fnGetTopic();

    });

    // 设置该评论为最佳评论
    function fnGetTopic() {
        var topic_id = fnGetUrlQueryString('topic_id');
        $.ajax({
            url: base_request_url + 'topics/' + topic_id,
            type: 'get',
            success: function (res) {
                console.log(res);
                if (res.code == '0') {
                    $('h4').text(res.data.id + '、' + res.data.title);

                    fnCreatWangEditor();

                } else {
                    // alert(res.msg);
                    fnShowErrorMsg(res.msg);
                }
            }
        })
    }

    function fnCreatWangEditor() {
        const E = window.wangEditor;
        editor = new E('#wangEditor');
        editor.create()
    }

    function fnShowErrorMsg(msg) {
        $('#topTips').text(msg).addClass('topTips-show').removeClass('topTips-hidden');
        setTimeout(function () {
            $('#topTips').removeClass('topTips-show').addClass('topTips-hidden');
        }, 3000);
    }

    function fnPostReply() {

        var topic_id = fnGetUrlQueryString('topic_id');
        var content = editor.txt.html();
        if (topic_id <= 0) {
            fnShowErrorMsg('话题ID错误');
            return;
        }
        if (!content) {
            fnShowErrorMsg('请输入回复内容');
            return;
        }

        $.ajax({
            url: base_request_url + 'replies',
            type: 'post',
            data: {topic_id: topic_id, content: content},
            success: function (res) {
                console.log(res);

                if (res.code == '0') {

                    history.back();

                } else {
                    // alert(res.msg);
                    fnShowErrorMsg(res.msg);
                }

            }
        })

    }
</script>


</html>
